<?php

class UIObject {
	private $__object;
	private $__name;
	public function __construct(MetaClass $c,$object,$name){
		$this->__object = $object;
		$this->__name = $name;
		for ($cc = $c; !is_null($cc); $cc = $cc->GetParentMetaClass()){
			foreach ($cc as $key=>$value){
				if ($value instanceof MetaField){
					$this->$key = new UIField($this,$value);
				}
				elseif ($value instanceof MetaSlave){
					$this->$key = new UIField($this,$value);
				}
			}
		}
	}

	public function GetObject(){ return $this->__object; }
	public function GetName(){ return $this->__name; }

	public function Read(Http $http, $control_classname=null){
		if (is_null($control_classname)){
			foreach ($this->GetUIFields() as $f){
				$n = $f->GetName();
				if (isset($http[$n])) {
					$f->SetValue( $http[$n]->CastTo($f->GetType()) );
				}
			}
		}
		else {
			$c = new $control_classname($this);
			$c->Read($http);
		}
	}


	public function GetUIFields(){
		$r = array();
		foreach ($this as $key=>$value) {
			if ($value instanceof UIField) {
				$r[] = $value;
			}
		}
		return $r;
	}
}


?>